package com.zwy.linear.impl;

import com.zwy.linear.LinearList;
import com.zwy.linear.Strategy;
/*
    数组实现线性表
 */
public class ListArray implements LinearList {
    private final int LEN=10;  //数组默认大小，线性表最大长度
    private Strategy strategy; //数据元素的默认比较策略
    private int size=0;  //线性表中元素个数
    private Object[] elements; //数据元素数组

    //构造方法
    public ListArray() {
    }
    //初始化数组元素个数为0,数组长度为LEN
    public ListArray(Strategy strategy) {
        this.strategy = strategy;
        this.size=0;
        elements=new Object[LEN];
    }

    @Override
    public int getSize() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
      return size==0;
    }

    @Override
    public boolean contains(Object e) {
        for (int i = 0; i < size; i++) {
            if(strategy.equal(elements[i],e)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(Object e) {
        for (int i = 0; i < size; i++) {
            if(strategy.equal(elements[i],e)){
                return i;
            }
        }
        return -1;
    }

    @Override
    public void insert(int i, Object e) throws IndexOutOfBoundsException {
      if(i<0||i>size){
          throw  new IndexOutOfBoundsException("序号越界");
      }
      if(size>=elements.length){
          expandSpace();
      }
      for (int j=size;j>i;j--){
          elements[j]=elements[j-1];
      }
      elements[i]=e;
      size++;
      return;
    }

    /**
     * 拓展数组空间
     */
    public void expandSpace(){
        Object[]a=new Object[elements.length*2];
        for (int i = 0; i < elements.length; i++) {
            a[i]=elements[i];
        }
        elements=a;
    }


    @Override
    public boolean insertBefore(Object obj, Object e) {
        int i=indexOf(obj);
        if(i<0)return false;
        insert(i,e);
        return true;
    }

    @Override
    public boolean insertAfter(Object obj, Object e) {
        int i=indexOf(obj);
        if(i<0)return false;
        insert(i+1,e);
        return true;
    }

    @Override
    public Object remove(int i) throws IndexOutOfBoundsException {
        if(i<0||i>=size){
            throw  new IndexOutOfBoundsException("序号越界");
        }
        Object obj=elements[i];
        for (int j=i;j<size-1;j++){
            elements[j]=elements[j+1];
        }
        elements[--size]=null;
        return null;
    }

    @Override
    public boolean remove(Object e) {
        int i=indexOf(e);
        if(i<0)return false;
        remove(i);
        return true;
    }

    @Override
    public Object replace(int i,Object e) throws IndexOutOfBoundsException {
        if(i<0||i>=size){
            throw  new IndexOutOfBoundsException("序号越界");
        }
        Object obj = elements[i];
        elements[i]=e;
        return obj;
    }

    @Override
    public Object get(int i) throws IndexOutOfBoundsException {
        if(i<0||i>=size){
            throw  new IndexOutOfBoundsException("序号越界");
        }
        return elements[i];
    }
}
